package com.tongzhou.client;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import net.sf.json.JSONObject;

import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.account.dao.IAccountDAO;
import com.tongzhou.tongzhou.account.dao.impl.AccountDAO;
import com.tongzhou.tongzhou.account.model.Account;
import com.tongzhou.tongzhou.quot.dao.IBasePaperDAO;
import com.tongzhou.tongzhou.quot.dao.IPaperTypeCoreDAO;
import com.tongzhou.tongzhou.quot.dao.IPaperTypeDAO;
import com.tongzhou.tongzhou.quot.dao.IRegionDAO;
import com.tongzhou.tongzhou.quot.dao.impl.BasePaperDAO;
import com.tongzhou.tongzhou.quot.dao.impl.PaperTypeCoreDAO;
import com.tongzhou.tongzhou.quot.dao.impl.PaperTypeDAO;
import com.tongzhou.tongzhou.quot.dao.impl.RegionDAO;
import com.tongzhou.tongzhou.quot.model.BasePaper;
import com.tongzhou.tongzhou.quot.model.Paper;
import com.tongzhou.tongzhou.quot.model.PaperCore;
import com.tongzhou.tongzhou.quot.model.PaperType;
import com.tongzhou.util.Util;

//报价接口
public class ClientQuot {
	IPaperTypeDAO paperTypeDAO=new PaperTypeDAO();
	IRegionDAO regionDAO=new RegionDAO();
	IAccountDAO dao=new AccountDAO();
	IBasePaperDAO basePaperDAO=new BasePaperDAO();
	IPaperTypeCoreDAO paperTypeCoreDAO=new PaperTypeCoreDAO();
	public String getPaperList(String id,String n){
		StringBuffer str=new StringBuffer();
		try {
			str.append("{'name':'"+n+"',");
			str.append("'priceList':[");
			List<Paper> papers=dao.getPaperList(id);
			for(int j=0;j<papers.size();j++){
				Paper p=papers.get(j);
				str.append("{'paper':'"+p.getPaper_name()+"',");
				str.append("'price':'"+p.getPrice()+"',");
				str.append("'brandList':[");
				List<BasePaper> bps=basePaperDAO.getBasePaperList(p.getPaper_name());
				for(int i=0;i<bps.size();i++){
					BasePaper bp=bps.get(i);
					str.append("{'brand':'"+bp.getSupplier()+"','params':'");
					str.append("定量-"+bp.getDl()+";");
					str.append("实测克重平均值-"+bp.getSckzpjz()+";");
					str.append("横向环压-"+bp.getHxhy()+";");
					str.append("耐破强度-"+bp.getNpqd()+";");
					str.append("原纸规格-"+bp.getYzgg()+"'}");
					if(i<bps.size()-1){
						str.append(",");
					}
				}
				str.append("]}");
				if(j<papers.size()-1){
					str.append(",");
				}
			}
			str.append("]}");
		}catch (DBException e) {
			e.printStackTrace();
		}
		return str.toString();
	}
	
	public String getPaperTypeList(List<PaperType> paperTypes){
		StringBuffer str=new StringBuffer();
		Set<String> ptNames=new LinkedHashSet<String>();
		for(PaperType pt:paperTypes){
			if(pt.getPaper_type_name().contains("单坑")){
				ptNames.add(pt.getPaper_type_name().replaceAll("单坑", ""));
			}else if(pt.getPaper_type_name().contains("双坑")){
				ptNames.add(pt.getPaper_type_name().replaceAll("双坑", ""));
			}else{
				ptNames.add(pt.getPaper_type_name());
			}
		}
		int tt=0;
		for(String ptName:ptNames){
			//加，
			if(tt>0){
				str.append(",");
			}
			tt++;
			
			str.append("{'paperName':'"+Util.changeToObj(ptName)+"',");
			str.append("'paperList':[");
			int temp=0;
			for(PaperType pt:paperTypes){
				if(pt.getPaper_type_name().contains("单坑")){
					if(ptName.equals(pt.getPaper_type_name().replaceAll("单坑", ""))){
						//加，
						if(temp>0){
							str.append(",");
						}
						temp++;
						str.append(getPaperList(pt.getId(),"单坑"));
					}
				}else if(pt.getPaper_type_name().contains("双坑")){
					if(ptName.equals(pt.getPaper_type_name().replaceAll("双坑", ""))){
						if(temp>0){
							str.append(",");
						}
						temp++;
						str.append(getPaperList(pt.getId(),"双坑"));
					}
				}else{
					if(ptName.equals(pt.getPaper_type_name())){
						if(temp>0){
							str.append(",");
						}
						temp++;
						str.append(getPaperList(pt.getId(),""));
					}
				}
				if(temp>=2){
					break;
				}
			}
			str.append("]}");
		}
		return str.toString();
	}
	
	public String getPaperCoreList(String type){
		StringBuffer str=new StringBuffer();
		try {
//			str.append(",");
			if(type.equals("1")){
				str.append("{'paperName':'变换中隔',");
				str.append("'paperList':[");
				str.append("{'name':'变换中隔',");
			}else{
				str.append("{'paperName':'变换加坑',");
				str.append("'paperList':[");
				str.append("{'name':'变换加坑',");
			}
			str.append("'priceList':[");
			List<PaperCore> paperCores=paperTypeCoreDAO.getPaperCoreList(type);
			for(int j=0;j<paperCores.size();j++){
				PaperCore p=paperCores.get(j);
				str.append("{'paper':'"+p.getPaper_core_name()+"',");
				str.append("'price':'"+p.getPrice()+"',");
				str.append("'brandList':[");
				List<BasePaper> bps=basePaperDAO.getBasePaperList(p.getPaper_core_name());
				for(int i=0;i<bps.size();i++){
					BasePaper bp=bps.get(i);
					str.append("{'brand':'"+bp.getSupplier()+"','params':'");
					str.append("定量-"+bp.getDl()+";");
					str.append("实测克重平均值-"+bp.getSckzpjz()+";");
					str.append("横向环压-"+bp.getHxhy()+";");
					str.append("耐破强度-"+bp.getNpqd()+";");
					str.append("原纸规格-"+bp.getYzgg()+"'}");
					if(i<bps.size()-1){
						str.append(",");
					}
				}
				str.append("]}");
				if(j<paperCores.size()-1){
					str.append(",");
				}
			}
			str.append("]}");
			str.append("]}");
//			str.append("]}");
			
		} catch (DBException e) {
			e.printStackTrace();
		}
		return str.toString();
	}
	
	public String getInfo(String json){
		JSONObject jsonObject = JSONObject.fromObject( json );
		Integer type=null;
		String rid=null;
		String companyNum=null;
		if(jsonObject.get("type")!=null){
			type=(Integer) jsonObject.get("type");
		}
		if(jsonObject.get("companyNum")!=null){
			companyNum=(String) jsonObject.get("companyNum");
		}
		if(jsonObject.get("rid")!=null){
			rid=(String) jsonObject.get("rid");
		}
		StringBuffer str=new StringBuffer();
		if(type==null||((companyNum==null||companyNum.equals(""))&&(rid==null||rid.equals("")))){
			return "{'success':0,'msg':'输入参数有误!'}";
		}else{
			try {
				if(companyNum!=null&&!companyNum.equals("")){
					Account account;
					try {
						account = dao.getAccountByNum(companyNum);
						if(account!=null){
							str.append("{'success':1,'msg':[");
							List<PaperType> paperTypes=dao.getPaperTypeList(account.getId());
							str.append(getPaperTypeList(paperTypes));
							str.append(",");
							str.append(getPaperCoreList("1"));
							str.append(",");
							str.append(getPaperCoreList("2"));
							str.append("]}");
						}else{
							str.append("{'success':0,'msg':'没有对应的报价信息!'}");
						}
						return str.toString();
					} catch (DBException e) {
						e.printStackTrace();
					}
				}
				if(rid!=null&&!rid.equals("")){
					str.append("{'success':1,'msg':[");
					List<PaperType> paperTypes=regionDAO.getPaperTypeList(rid);
					str.append(getPaperTypeList(paperTypes));
					str.append("]}");
				}
				return str.toString();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "{'success':0,'msg':'输入参数有误!'}";
	}
}
